﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Text;
using Thor.Domain;
using Thor.Domain.Configuration;

namespace Thor.Repository.EntFx
{
    public class SettingsRepository : RepositoryBase, ISettingsRepository
    {
        #region Constructors

        public SettingsRepository(Dictionary<string, object> repositoryParameters)
            : base(repositoryParameters) { }

        #endregion

        #region ISettingsRepository Members

        public void Insert(Settings settings)
        {
            if (settings != null && Model != null)
            {
                var entity = CreateEntityFromPoco(settings);
                Model.AddToSettingsEntities(entity);
                Model.SaveChanges();
            }
        }

        public void Update(Settings settings)
        {
            if (settings != null && Model != null)
            {
                Delete(settings);
                Insert(settings);
            }
        }

        public void Delete(Settings settings)
        {
            if (settings != null && Model != null)
            {
                var entity = Model.SettingsEntities.FirstOrDefault();

                if (entity != null)
                {
                    Model.DeleteObject(entity);
                    Model.SaveChanges();
                }
            }
        }

        public Settings Fetch()
        {
            if (Model != null)
            {
                var entity = Model.SettingsEntities.FirstOrDefault();

                if (entity != null)
                    return CreatePocoFromEntity(entity);
                else
                    return null;
            }

            return null;
        }

        #endregion

        #region Methods

        private SettingsEntity CreateEntityFromPoco(Settings poco)
        {
            SettingsEntity entity = new SettingsEntity();

            entity.ApplicationName = ApplicationName;
            entity.AssemblyName = poco.AssemblyName;
            entity.AssemblyType = poco.AssemblyType;
            entity.BookingAgentEmail = poco.BookingAgentEmail;
            entity.CalendarProviderUri = poco.CalendarProviderUri;
            entity.IsEncrypted = poco.IsEncrypted;
            entity.LockoutDuration = poco.LockoutDuration;
            entity.Password = poco.Password;
            entity.RetryCount = poco.RetryCount;
            entity.RetryInterval = poco.RetryInterval;
            entity.ServerTimeZone = poco.ServerTimeZone;
            entity.UseBookingAgent = poco.UseBookingAgent;
            entity.UserName = poco.UserName;
            entity.IsEncrypted = poco.IsEncrypted;

            return entity;
        }

        private Settings CreatePocoFromEntity(SettingsEntity entity)
        {
            Settings poco = new Settings(entity.IsEncrypted);

            poco.AssemblyName = entity.AssemblyName;
            poco.AssemblyType = entity.AssemblyType;
            poco.BookingAgentEmail = entity.BookingAgentEmail;
            poco.CalendarProviderUri = entity.CalendarProviderUri;
            poco.LockoutDuration = entity.LockoutDuration;
            poco.Password = entity.Password;
            poco.RetryCount = entity.RetryCount;
            poco.RetryInterval = entity.RetryInterval;
            poco.ServerTimeZone = entity.ServerTimeZone;
            poco.UseBookingAgent = entity.UseBookingAgent;
            poco.UserName = entity.UserName;

            return poco;
        }

        #endregion
    }
}
